﻿// 用于文件上传，在相关页面需要添加如下标签，详细操作请参照Device/frmDeviceTypeBody.aspx
//<input type="hidden" id="fileCount" runat="server" />
//<input type="hidden" id="delFile" runat="server" />
//<input id="limit" type="hidden" value="3" />

var filecnt = 2;
var limit = 3;
var fv = "ctl00_ContentPlaceHolder_frmRecordView_Upload1_";
//限制上传的文件类型，请保留最后一个分号
var fileType = "txt;doc;xls;html;htm;jpg;gif;ppt;msg;pdf;";

function SetClass(aa, bn) {
    if (arguments.length == 1) {
        var bn = aa;
        var aa = event.srcElement;
    }
    aa.className = bn;
    if (document.getElementById("limit") != null && limit < 0)
        limit = parseInt(document.getElementById("limit").value);
    var hrefAddFile = document.getElementById("hrefAddFile");
    if (GetCount() >= limit && document.getElementById("alertButton") == null) {
        var button = document.createElement("input");
        button.id = "alertButton";
        button.className = "addfile";
        button.type = "button";
        button.onclick = FileFull;
        button.hideFocus = true;
        hrefAddFile.appendChild(button);
    }
}
//取得上传文件总数
function GetCount() {
    var fv = "ctl00_ContentPlaceHolder_frmRecordView_";
    if (document.getElementById(fv + "fileupload_fileCount") == null ||
        document.getElementById(fv + "fileupload_fileCount").value == "") {
        return 0;
    }
    else {
        return parseInt(document.getElementById(fv + "fileupload_fileCount").value);
    }
}
//设置上传文件总数，varCount为在原有上传文件总数应该增加或者减少的文件数，在新增或者删除文件后调用。
function SetCount(varCount) {
    var count = 0;
    var fv = "ctl00_ContentPlaceHolder_frmRecordView_";
    if (document.getElementById(fv + "fileupload_fileCount").value != "") {
        count = parseInt(document.getElementById(fv + "fileupload_fileCount").value);
        document.getElementById(fv + "fileupload_fileCount").value = (count + varCount).toString();
    }
    else {
        if (varCount < 0)
            document.getElementById(fv + "fileupload_fileCount").value = "0";
        else
            document.getElementById(fv + "fileupload_fileCount").value = varCount.toString();
    }
}
function AddFile(input) {
    //屏蔽因焦点失去而导致的冲突
    if (event.propertyName.toUpperCase() == "TABINDEX") {
        return;
    }
    if (arguments.length == 0) {
        input = event.srcElement;
    }
    var errorMsg = "";
    //判断添加的文件是否已存在
    if (IsExist(input)) {
        errorMsg += "文件已添加，不允许重复添加同一个文件！\r\n";
    }
    //检查文件扩展名
    if (!CheckExtension(input)) {
        errorMsg += "上传的文件类型仅限于：" + fileType.substring(0, fileType.length - 1) + "。\r\n";
    }
    //判断上传的文件是否大于10MB
    //    if(!ShowSize(input.value))
    //    {
    //        errorMsg += "对不起，上传的文件不能大于10MB！\r\n";
    //    }
    if (errorMsg != "") {
        alert(errorMsg);
        return;
    }
    //显示添加的附件
    AddLstFile(input);
    //去掉其事件
    input.onpropertychange = null;
    //添加新的上传
    CreateChild();
}
//检查文件扩展名
function CheckExtension(input) {
    var fileName = input.value;
    if (fileName != null && fileName.lastIndexOf('.') != -1) {
        var type = fileName.substring(fileName.lastIndexOf('.') + 1) + ";";
        if (fileType.indexOf(type.toLowerCase(), 0) == -1)
            return false;
        else
            return true;
    }
    return false;
}
//判断选定的文件是否已存在
function IsExist(input) {
    var sFileName = input.value;
    var sID = input.id;

    var lstAddfile = document.getElementsByTagName("input");
    for (i = 0; i < lstAddfile.length; i++) {
        if (lstAddfile[i].type == "file" && lstAddfile[i].id != sID) {
            if (lstAddfile[i].value.toUpperCase() == sFileName.toUpperCase()) {
                return true;
            }
        }
    }
    return false;
}

//判断上传文件的大小
function ShowSize(filePath) {
    var fso, file, flag = false;
    if (filePath != "") {
        fso = new ActiveXObject("Scripting.FileSystemObject");
        file = fso.GetFile(filePath);
        if ((file.size / (1024 * 1024)) <= 10)
            flag = true;
    }
    return flag;
}

//将文件显示出来
function AddLstFile(input) {
    var sFileName = input.value;
    //得到文件名，而不显示完整名称
    var sSpet = "\\";
    var nPos = sFileName.lastIndexOf(sSpet);
    var sTmp = sFileName;
    //alert(nPos);
    if (nPos > 0) {
        sTmp = sFileName.substring(nPos + 1, sFileName.length);
        //alert(sTmp);
    }

    //产生列表
    var sInnerHtml = '<TABLE class="cmf_tb" attachType="upload" style="float:left;" id="DispFile' + filecnt + '"><TR><TD class="btnSicoLcf"></TD><TD><NOBR><a id=' + "hrefClick" + ' href=' + "#" + ' onclick=' + "OpenFile('" + escape(input.value) + "')" + '>' +
        sTmp + '</a></NOBR></TD><TD><A class="btnCls3" title="删除"  onclick="return DeleteFile(this)"' +
        'InputID="' + input.id + '" TableID="DispFile' + filecnt + '"' +
        ' onmouseover="SetClass(' + "'btnCls3Over'" + ')"' +
        ' onmouseout="SetClass(' + "'btnCls3'" + ')"' +
        '></A></TD><TD>;</TD></TR></TABLE>';
    var listfile = document.getElementById("listfile");
    listfile.innerHTML = listfile.innerHTML + sInnerHtml;
}
//打开/浏览文件的方法
function OpenFile(filePath) {
    var conType = GetContentType(unescape(filePath))
    if (conType != "")
        window.open("../OpenFiles.aspx?path=" + filePath + "&contenttype=" + escape(conType) + "");
    else {
        alert("不支持此文件的打开方式,请在'" + unescape(filePath) + "'路径下打开");
        return false;
    }
}
function GetContentType(fileName) {
    var result = "";
    if (fileName != null && fileName.lastIndexOf('.') != -1) {
        var type = fileName.substring(fileName.lastIndexOf('.'));
        switch (type.toLowerCase()) {
            case ".txt":
            case ".doc":
                result = "Application/msword";
                break;
            case ".xls":
                result = "Application/x-msexcel";
                break;
            case ".vsd":
                result = "application/vnd.visio";
                break;
            case ".mpp":
                result = "application/vnd.ms-project";
                break;
            case ".pdf":
                result = "Application/pdf";
                break;
        }
    }
    return result;
}
//在其中添加一个新的Input type=file
function CreateChild() {
    var newInput = document.createElement("input");
    newInput.name = "";
    newInput.id = "fileUpload" + (filecnt++);
    newInput.className = "addfile";
    newInput.type = "file";
    newInput.size = "1";
    newInput.hideFocus = true;
    //添加事件处理
    newInput.onkeydown = new Function("return false");
    newInput.onpropertychange = AddFile;
    //newInput.onchange=AddFile;
    var hrefAddFile = document.getElementById("hrefAddFile");
    hrefAddFile.appendChild(newInput);

    //增加上传文件数
    SetCount(+1);

    if (GetCount() >= limit) {
        var button = document.createElement("input");
        button.id = "alertButton";
        button.className = "addfile";
        button.type = "button";
        button.onclick = FileFull;
        button.hideFocus = true;
        hrefAddFile.appendChild(button);
    }
}

//删除选中的附件内容
function DeleteFile(SrcEle) {
    if (arguments.length == 0) {
        SrcEle = event.srcElement;
    }

    //得到要删除的ＩＤ
    //SrcEle.InputID;
    var hrefAddFile = document.getElementById("hrefAddFile");
    hrefAddFile.removeChild(document.getElementById(SrcEle.InputID));
    var listfile = document.getElementById("listfile");
    listfile.removeChild(document.getElementById(SrcEle.TableID));
    //增加上传文件数
    SetCount(-1);

    if (document.getElementById("alertButton") != null)
        hrefAddFile.removeChild(document.getElementById("alertButton"));
    return false;
}

function DeleteSavedFile(SrcEle) {
    if (arguments.length == 0) {
        SrcEle = event.srcElement;
    }

    var hrefAddFile = document.getElementById("hrefAddFile");
    var fileEdit = document.getElementById("fileEdit");
    var delFile = document.getElementById("delFile");
    if (delFile.value == null || delFile.value == "")
        delFile.value = SrcEle.fileName + ";";
    else
    //delFile.value = delFile.value + SrcEle.fileName + ";";
        delFile.value += SrcEle.fileName + ";";
    fileEdit.removeChild(document.getElementById(SrcEle.TableID));

    SetCount(-1);

    if (document.getElementById("alertButton") != null)
        hrefAddFile.removeChild(document.getElementById("alertButton"));
    return false;
}

function fgoto()
{ }

function FileFull() {
    var alertString = "最多只能添加" + limit.toString() + "个附件";
    alert(alertString);
}

function valFile_Validation(source, args) {
    if (GetCount() <= 0)
        args.IsValid = false;
    else
        args.IsValid = true;
}